//
//  JGGView.m
//  WisdomCommunity
//
//  Created by Yahaong on 16/7/29.
//  Copyright © 2016年 zonten. All rights reserved.
//

#import "JGGView.h"
#import "YHMarcros.h"
@implementation JGGView

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource completeBlock:(TapBlcok )tapBlock
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}
-(void)JGGView:(JGGView *)jggView DataSource:(NSArray *)dataSource completeBlock:(TapBlcok)tapBlock
{
    for (NSUInteger i=0; i<dataSource.count; i++) {
        UIImageView *iv = [UIImageView new];
        if ([dataSource[i] isKindOfClass:[UIImage class]]) {
            iv.image = dataSource[i];
        }else if ([dataSource[i] isKindOfClass:[NSString class]]){
            iv.image = [UIImage imageNamed:dataSource[i]];
        }else if ([dataSource[i] isKindOfClass:[NSURL class]]){
            
        }
        jggView.dataSource = dataSource;
        jggView.tapBlock = tapBlock;
        iv.userInteractionEnabled = YES;//默认关闭NO，打开就可以接受点击事件
        iv.tag = i;
        [jggView addSubview:iv];
        //九宫格的布局
        CGFloat  Direction_X = (([JGGView imageWidth]+kJGG_GAP)*(i%3));
        CGFloat  Direction_Y  = (floorf(i/3.0)*([JGGView imageHeight]+kJGG_GAP));
        
        [iv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(jggView).offset(Direction_X);
            make.top.mas_equalTo(jggView).offset(Direction_Y);
            make.size.mas_equalTo(CGSizeMake([JGGView imageWidth], [JGGView imageHeight]));
        }];
        
//        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:jggView action:@selector(tapImageAction:)];
//        [iv addGestureRecognizer:singleTap];
    }
    
}
#pragma mark 配置图片的宽高
+(CGFloat)imageWidth{
    return ([UIScreen mainScreen].bounds.size.width-(2*kGAP+kAvatar_Size)*2)/3;
}
+(CGFloat)imageHeight{
    return ([UIScreen mainScreen].bounds.size.width-(2*kGAP+kAvatar_Size)*2)/3;
}
@end
